package com.globallogic.vkulyk.SSHTool;

import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CLIUserDataModeHandler {
	private String[] args = null;
	private String host = "";
	private String user = "";
	private String password = "";
	private int port = 22;
	private String actionType = "";
	private String command = "";
	private String script = "";
	private String srcFile = "";
	private String dstDir = "";

	private final Logger logger = LoggerFactory
			.getLogger(CLIUserDataModeHandler.class);

	public CLIUserDataModeHandler(String[] args) {
		this.args = args;
	}

	public void run() {
		getDataFromCLI();
		callExecutor();
	}

	private void getDataFromCLI() {
		logger.debug("Parsing arguments..");
		String argsInOneString = "";
		for (String arg : args) {
			argsInOneString = argsInOneString.concat(" " + arg);
		}
		logger.debug("Arguments entered: '{}'",
				argsInOneString.replaceFirst(":.*@", ":*****@"));
		// oracle:orapass@172.0.0.102:22 -c .. / -s .. /-put/get ..
		String SSH_URIPattern = ".\\S+:\\S+@\\S+.";
		if (!Pattern.compile(SSH_URIPattern).matcher(args[0]).matches()) {
			logger.error(
					"ERROR: Illegal URI '{}'. Must be: 'user:password@host' or 'user:password@host:port'.  Exiting.",
					args[0]);
			System.exit(1);
		}
		String[] aURI = args[0].split(":");
		user = aURI[0];
		password = aURI[1].replaceFirst("@.*$", "");
		host = aURI[1].replaceFirst("^.*@", "");
		if (aURI.length == 3) {
			port = Integer.parseInt(aURI[2]);
		}
		if (argsInOneString.contains(" -c ")) {
			actionType = "execute_command";
			command = argsInOneString.replaceFirst("^.* -c ", "").trim();
		} else if (argsInOneString.contains(" -s ")) {
			actionType = "execute_script";
			script = argsInOneString.replaceFirst("^.* -s ", "").trim();
			// -put/get /dir1/01.txt /dir2
		} else if (argsInOneString.contains(" -put ")
				|| argsInOneString.contains(" -get ")) {
			if (argsInOneString.contains(" -put ")) {
				actionType = "put";
			} else if (argsInOneString.contains(" -get ")) {
				actionType = "get";
			}
			String[] copyOptions = argsInOneString
					.replaceFirst("^.* -" + actionType + " ", "").trim()
					.split("\\s+");
			if (copyOptions.length == 2) {
				srcFile = copyOptions[0];
				dstDir = copyOptions[1];
			} else {
				logger.error(
						"ERROR: 2 arguments (source file and destination directory) are expected. You entered: '{}'. Exiting.",
						argsInOneString);
				System.exit(1);
			}
		}
	}

	private void callExecutor() {
		logger.debug("Calling executor..");
		if (actionType.equals("execute_command")) {
			new CommandExecutor(host, port, user, password, command).execute();
		} else if (actionType.equals("put")) {
			new FilePutter(host, port, user, password, srcFile, dstDir)
					.execute();
		} else if (actionType.equals("put") || actionType.equals("get")) {
			new FileGetter(host, port, user, password, srcFile, dstDir)
					.execute();
		} else if (actionType.equals("execute_script")) {
			new ScriptExecutor(host, port, user, password, script).execute();
		}
	}

}
